package com.shanshi.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.shanshi.activity.adapter.BillDetailAdapter;
import com.shanshi.activity.response.Bills;
import com.shanshi.activity.tool.CustomRefreshListView;
import com.shanshi.activity.tool.OkHttpTool;

import java.util.ArrayList;
import java.util.List;


public class BillDetails extends MainActivity implements OnItemClickListener,OnScrollListener{

	private SharedPreferences sharedPreferences;
	private int userId;
	private TextView tv;
	private ImageView Iv;
	private CustomRefreshListView listView;
	private BillDetailAdapter myAdapter;
	private List<Bills.Data> list;
	private List<Bills.Data> listdata;
	private int pageIndex=0;
	private TextView textSum;

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.bill_details);

		sharedPreferences = BillDetails.this.getSharedPreferences("share", MODE_PRIVATE);
		userId = sharedPreferences.getInt("userId",29);

		tv = (TextView) findViewById(R.id.title);	
		tv.setText("交易明细");
		
		Iv = (ImageView) findViewById(R.id.fanhui);
		Iv.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		listView = (CustomRefreshListView) findViewById(R.id.bill_detail);

		list = new ArrayList<Bills.Data>();
		listdata = new ArrayList<Bills.Data>();
		myAdapter = new BillDetailAdapter(this, listdata);
		listView.setAdapter(myAdapter);
		getdatas();

		listView.setOnRefreshListener(new CustomRefreshListView.OnRefreshListener() {

			@Override
			public void onPullRefresh() {

				Handler handler = new Handler();
				handler.postDelayed(new Runnable() {
					@Override
					public void run() {
						listdata.clear();
						pageIndex = 0;
						getdatas();
						listView.completeRefresh();
					}
				}, 2000);//2秒后执行Runnable中的run方法
			}

			@Override
			public void onLoadingMore() {
				pageIndex++;
				getdatas();
			}
		});

		listView.setOnItemClickListener(this);
		listView.setOnScrollListener(this);
	}


	//获取数据
	public void getdatas(){
		OkHttpTool.doGet(OkHttpTool.BASE_URL+"users/"+userId+"/bills?pageIndex="+pageIndex+"&pageSize=4"
				, new OkHttpTool.setOkhttpListener() {
					@Override
					public void onSuccess(String s) {
						// TODO Auto-generated method stub
						list.clear();
						Gson gson=new Gson();
						Bills content=gson.fromJson(s, Bills.class);
						Log.e("bills",content.toString());
						list.addAll(content.getData());
						handler.sendEmptyMessage(1);
					}

					@Override
					public void onFail() {
						// TODO Auto-generated method stub
						Log.e("=========我的账单请求失败","sadfa");
					}
				});

	}

	Handler handler=new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
				case 1:
					listdata.addAll(list);
					myAdapter.notifyDataSetChanged();
					break;
			}
		}
	};


	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Intent intent = new Intent(BillDetails.this,BillDetailed.class);
		intent.putExtra("billId",listdata.get(position-1).getId());
		startActivity(intent);
		
	}
	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
		// TODO Auto-generated method stub
		
	}

}
